package com.tianyang.yukuaifu.business.auth.view;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.facebook.drawee.view.SimpleDraweeView;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.business.auth.bean.BankRegOutput;
import com.tianyang.yukuaifu.business.auth.bean.BusinessOutput;
import com.tianyang.yukuaifu.business.auth.bean.CompanySettleInput;
import com.tianyang.yukuaifu.business.auth.bean.IndividualSettleInput;
import com.tianyang.yukuaifu.business.auth.bean.SampleImageOutput;
import com.tianyang.yukuaifu.business.auth.contract.AuthConfig;
import com.tianyang.yukuaifu.common.cache.CacheManage;
import com.tianyang.yukuaifu.common.cache.CacheModel;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.common.utils.SingleCheckListener;
import com.tianyang.yukuaifu.module.dialog.SampleImageDialog;
import com.tianyang.yukuaifu.utils.SoftInputUtil;
import com.tianyang.yukuaifu.utils.ToastUtil;
import com.tianyang.yukuaifu.utils.VerifyUtil;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description：个体工商户结算信息
 * Created by xyj on 2024/3/10
 */
public class AuthIndividualSettleView extends LinearLayout {

    @BindView(R.id.bankFrontImgSdv)
    SimpleDraweeView bankFrontImgSdv;
    @BindView(R.id.bankcardidEt)
    EditText bankcardidEt;
    @BindView(R.id.bankNameEt)
    EditText bankNameEt;
    @BindView(R.id.openingBankTv)
    TextView openingBankTv;
    @BindView(R.id.bankTelEt)
    EditText bankTelEt;
    @BindView(R.id.nextStepSettleTv)
    Button nextStepSettleTv;

    @BindView(R.id.settleWayLl)
    LinearLayout settleWayLl;
    @BindView(R.id.settleWayTv)
    TextView settleWayTv;
    @BindView(R.id.legalPersonLl)
    LinearLayout legalPersonLl;
    @BindView(R.id.unInCorporateLl)
    AuthUnInCorporateView unInCorporateLl;

    private SampleImageDialog vSampleImageDialog;

    private SingleCheckListener vSingleCheckListener;

    private IndividualSettleInput mIndividualSettleInput;

    private SampleImageOutput mOutput;

    public AuthIndividualSettleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(getContext(), R.layout.view_auth_individual_settle, this);
        ButterKnife.bind(this);
        mIndividualSettleInput = new IndividualSettleInput();
        mIndividualSettleInput.setSettleWay(AuthConfig.SETTLE_WAY_FR);
        mOutput = CacheManage.getInstance().getCache(CacheModel.SAMPLE_IMAGE);
    }

    @OnClick({R.id.takeBankFrontTv, R.id.settleWayTv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.takeBankFrontTv:
                ToastUtil.show("查看银行卡示例");
                if (mOutput != null) {
                    if (vSampleImageDialog == null) {
                        vSampleImageDialog = new SampleImageDialog(getContext());
                    }
                    vSampleImageDialog.showPictureDataView(mOutput.getBankcardpic());
                    vSampleImageDialog.show();
                }
                break;
            case R.id.settleWayTv:
                if (vSingleCheckListener == null) {
                    vSingleCheckListener = new SingleCheckListener((Activity) getContext());
                }
                vSingleCheckListener.handleCheck(view, settleWayTv, Arrays.asList("法人", "非法人"),
                        new SingleCheckListener.IWheelDataChangeCallback() {
                            @Override
                            public void wheelDataChangeCallback(int position) {
                                mIndividualSettleInput.setSettleWay(position == 0 ? AuthConfig.SETTLE_WAY_FR : AuthConfig.SETTLE_WAY_FFR);
                                if (position == 0) {
                                    legalPersonLl.setVisibility(View.VISIBLE);
                                    unInCorporateLl.setVisibility(View.GONE);
                                } else {
                                    legalPersonLl.setVisibility(View.GONE);
                                    unInCorporateLl.setVisibility(View.VISIBLE);
                                }
                            }
                        });
                break;
        }
    }

    public void onFeedBackView(IndividualSettleInput input, String posChannel) {
        if (TextUtils.equals(AuthConfig.POS_CHANNEL_FY, posChannel)) {
            settleWayLl.setVisibility(View.VISIBLE);
        } else {
            settleWayLl.setVisibility(View.GONE);
        }
        if (input != null) {
            mIndividualSettleInput = input;
            if (TextUtils.equals(AuthConfig.SETTLE_WAY_FFR, input.getSettleWay())) {
                settleWayTv.setText("非法人");
            } else {
                settleWayTv.setText("法人");
            }
            if (TextUtils.equals(AuthConfig.SETTLE_WAY_FFR, input.getSettleWay())) {
                legalPersonLl.setVisibility(View.GONE);
                unInCorporateLl.setVisibility(View.VISIBLE);
            } else {
                legalPersonLl.setVisibility(View.VISIBLE);
                unInCorporateLl.setVisibility(View.GONE);
            }
            unInCorporateLl.onFeedBackView(JSON.parseObject(JSON.toJSONString(mIndividualSettleInput), CompanySettleInput.class));

            if (!TextUtils.isEmpty(mIndividualSettleInput.getBankcardpic()))
                bankFrontImgSdv.setImageURI(Uri.parse(mIndividualSettleInput.getBankcardpic()));
            bankcardidEt.setText(VerifyUtil.checkString(mIndividualSettleInput.getBankcardid()));
            bankNameEt.setText(VerifyUtil.checkString(mIndividualSettleInput.getBankName()));
            openingBankTv.setText(VerifyUtil.checkString(mIndividualSettleInput.getOpeningBank()));
            bankTelEt.setText(VerifyUtil.checkString(mIndividualSettleInput.getBankTel()));
        }
    }

    public void showBankCardInfoView(BankRegOutput output, String currentPhotoType) {
        if (output != null) {
            if (TextUtils.equals(CommonConfig.TYPE_UNBANKCARDPIC, currentPhotoType)) {
                unInCorporateLl.showBankCardInfoView(output);
            } else {
                bankNameEt.setText(VerifyUtil.checkString(output.getBank_name()));
                mIndividualSettleInput.setBankName(output.getBank_name());
                if (!TextUtils.isEmpty(output.getBank_card_number())) {
                    String bankCardId = output.getBank_card_number().replaceAll(" ", "");
                    bankcardidEt.setText(bankCardId);
                    mIndividualSettleInput.setBankcardid(bankCardId);
                }
            }
        }
    }

    public void showPhotoView(String urlStr, String photoType) {
        switch (photoType) {
            case CommonConfig.TYPE_CRDFROPIC:
                bankFrontImgSdv.setImageURI(Uri.parse(urlStr));
                mIndividualSettleInput.setBankcardpic(urlStr);
                break;
            default:
                unInCorporateLl.showPhotoView(urlStr, photoType);
                break;
        }
    }

    public void showBusinessView(BusinessOutput output, boolean isUnInCorporate) {
        if (output != null) {
            if (isUnInCorporate) {
                unInCorporateLl.showBusinessView(output);
            } else {
                openingBankTv.setText(VerifyUtil.checkString(output.getBranchBankName()));
                mIndividualSettleInput.setOpeningBank(output.getBranchBankName());
                mIndividualSettleInput.setBranchBankNo(output.getBranchNo());
            }
        }
    }

    public IndividualSettleInput getIndividualSettleInput() {
        if (TextUtils.equals(AuthConfig.SETTLE_WAY_FFR, mIndividualSettleInput.getSettleWay())) {
            return JSON.parseObject(JSON.toJSONString(unInCorporateLl.getSettleInput()), IndividualSettleInput.class);
        } else {
            mIndividualSettleInput.setBankcardid(bankcardidEt.getText().toString());
            mIndividualSettleInput.setBankName(bankNameEt.getText().toString());
            mIndividualSettleInput.setBankTel(bankTelEt.getText().toString());
            return mIndividualSettleInput;
        }
    }

    public boolean checkCompanySettle() {
        if (TextUtils.equals(AuthConfig.SETTLE_WAY_FFR, mIndividualSettleInput.getSettleWay())) {
            return unInCorporateLl.checkSettleInfo();
        } else {
            if (TextUtils.isEmpty(mIndividualSettleInput.getBankcardpic())) {
                ToastUtil.show("请上传银行卡卡号照");
                return false;
            }
            if (TextUtils.isEmpty(bankcardidEt.getText())) {
                ToastUtil.show("请填写银行卡号");
                SoftInputUtil.openKeybord(bankcardidEt, getContext());
                return false;
            }
            if (TextUtils.isEmpty(bankNameEt.getText())) {
                ToastUtil.show("请填写银行名称");
                SoftInputUtil.openKeybord(bankNameEt, getContext());
                return false;
            }
            if (TextUtils.isEmpty(mIndividualSettleInput.getOpeningBank())) {
                ToastUtil.show("请选择开户支行");
                return false;
            }
            if (TextUtils.isEmpty(bankTelEt.getText())) {
                ToastUtil.show("请填写预留手机号");
                SoftInputUtil.openKeybord(bankTelEt, getContext());
                return false;
            } else if (VerifyUtil.checkPhoneNumber(bankTelEt.getText())) {
                ToastUtil.show("请输入正确的手机号");
                SoftInputUtil.openKeybord(bankTelEt, getContext());
                return false;
            }
        }
        return true;
    }

    public void setOnlyReadView() {
        unInCorporateLl.setOnlyReadView();
        bankcardidEt.setEnabled(false);
        bankNameEt.setEnabled(false);
        openingBankTv.setEnabled(false);
        openingBankTv.setCompoundDrawables(null, null, null, null);
        bankTelEt.setEnabled(false);
        nextStepSettleTv.setVisibility(GONE);
        settleWayTv.setEnabled(false);
        settleWayTv.setCompoundDrawables(null, null, null, null);
    }

    public void onDestroyView() {
        if (unInCorporateLl != null) {
            unInCorporateLl.onDestroyView();
        }
        if (vSampleImageDialog != null && vSampleImageDialog.isShowing()) {
            vSampleImageDialog.dismiss();
            vSampleImageDialog = null;
        }
    }
}
